# syntax=docker/dockerfile:labs

ARG PHP_VERSION=7.2.34

FROM --platform=$TARGETPLATFORM php:${PHP_VERSION}-alpine as upstream

FROM --platform=$TARGETPLATFORM alpine:3.11

LABEL maintainer="khs1994-docker/lnmp <khs1994@khs1994.com>"

ENV TZ=Asia/Shanghai \
    APP_ENV=development

ARG PHP_VERSION=7.2.34

ARG ALPINE_URL=dl-cdn.alpinelinux.org

ARG PHP_EXTENSION_EXTRA

ARG PECL_EXTENSION_EXTRA

ARG APK_EXTRA="libzip libsodium"

ARG APK_DEV_EXTRA="libzip-dev libsodium-dev argon2-dev"

ARG PHP_EXTRA_CONFIGURE_ARGS_NEW="--with-sodium=shared \
                                  --with-password-argon2"

# dependencies required for running "phpize"
# these get automatically installed and removed by "docker-php-ext-*" (unless they're already installed)
ENV PHPIZE_DEPS \
      autoconf \
      dpkg-dev dpkg \
      file \
      g++ \
      gcc \
      libc-dev \
      make \
      pkgconf \
      re2c \
      bison

# persistent / runtime deps
RUN set -ex \
      && sed -i "s/dl-cdn.alpinelinux.org/${ALPINE_URL}/g" /etc/apk/repositories \
      && apk add --no-cache \
                 ca-certificates \
                 curl \
                 tar \
                 xz \
# https://github.com/docker-library/php/issues/494
                 openssl \
                 # git \
# 以下两个均为版本控制系统
# subversion \
# mercurial \
                 # openssh-client \
                 # tini \
                 bash \
                 tzdata \
# ensure www-data user exists
# RUN set -x \
      && addgroup -g 82 -S www-data \
      && adduser -u 82 -D -S -G www-data www-data
# 82 is the standard uid/gid for "www-data" in Alpine
# http://git.alpinelinux.org/cgit/aports/tree/main/apache2/apache2.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/lighttpd/lighttpd.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/nginx-initscripts/nginx-initscripts.pre-install?h=v3.3.2

ENV PHP_INI_DIR /usr/local/etc/php
# RUN mkdir -p $PHP_INI_DIR/conf.d

##<autogenerated>##
ENV PHP_EXTRA_CONFIGURE_ARGS \
        --disable-phpdbg \
        # --enable-fpm \
        # --with-fpm-user=www-data \
        # --with-fpm-group=www-data \
        # https://github.com/docker-library/php/issues/510
        --enable-embed=shared \
        --disable-cgi \
        --with-gettext=shared \
        --with-gd=shared \
            --with-freetype-dir=/usr \
            --disable-gd-jis-conv \
            --with-jpeg-dir=/usr \
            --with-png-dir=/usr \
            --with-webp-dir=/usr \
            --with-xpm-dir=/usr \
        --with-pcre-regex=/usr \
        --with-pdo-mysql=shared \
        --with-pdo-pgsql=shared \
        # --with-xsl=shared \
        --enable-bcmath=shared \
        --enable-pcntl=shared \
        --enable-shmop=shared \
        # --enable-soap=shared \
        --enable-sockets=shared \
        --enable-sysvmsg=shared \
        --enable-sysvsem=shared \
        --enable-sysvshm=shared \
        --enable-zip=shared \
            --with-libzip \
        --enable-calendar=shared \
        --enable-intl=shared \
        --enable-exif=shared \
        --with-bz2=shared \
        # --with-tidy=shared \
        --with-gmp=shared \
        --with-imap=shared \
            --with-kerberos \
            --with-imap-ssl \
        --with-icu-dir=/usr \
        # --with-xmlrpc=shared \
        --with-pic \
        --with-enchant=shared \
        --enable-fileinfo=shared \
        --with-ldap=shared \
            --with-ldap-sasl \
        --with-pspell=shared \
        --enable-shmop=shared \
        # --with-snmp=shared \
        --with-mysqli=shared \
        --with-pgsql=shared \
        --with-iconv=shared,/usr \
        --enable-ctype=shared \
        --enable-dom=shared \
        --enable-json=shared \
        --enable-pdo=shared \
        --with-pdo-sqlite=shared,/usr \
        --enable-phar=shared \
        --enable-posix=shared \
        --enable-session=shared \
        --enable-simplexml=shared \
        --with-sqlite3=shared,/usr \
        --enable-tokenizer=shared \
        --enable-xml=shared \
        --enable-xmlreader=shared \
        --enable-xmlwriter=shared \
        ${PHP_EXTRA_CONFIGURE_ARGS_NEW:-}
##</autogenerated>##

# Apply stack smash protection to functions using local buffers and alloca()
# Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
# Enable optimization (-O2)
# Enable linker optimization (this sorts the hash buckets to improve cache locality, and is non-default)
# Adds GNU HASH segments to generated executables (this is used if present, and is much faster than sysv hash; in this configuration, sysv hash is also generated)
# https://github.com/docker-library/php/issues/272
ENV PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
ENV PHP_CPPFLAGS="$PHP_CFLAGS"
ENV PHP_LDFLAGS="-Wl,-O1 -pie"

# ENV GPG_KEYS 1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F

ENV PHP_VERSION=${PHP_VERSION}

# ENV PHP_URL="https://www.php.net/get/php-7.2.34.tar.xz/from/this/mirror" PHP_ASC_URL="https://www.php.net/get/php-7.2.34.tar.xz.asc/from/this/mirror"
# ENV PHP_SHA256="da1a705c0bc46410e330fc6baa967666c8cd2985378fb9707c01a8e33b01d985" PHP_MD5=""

# COPY --from=php:alpine /usr/local/bin/docker-php-* /usr/local/bin/

# COPY --from=php:alpine /usr/local/bin/docker-php-source /usr/local/bin/
# COPY --from=php:alpine /usr/local/bin/docker-php-entrypoint /usr/local/bin/
# COPY --from=php:alpine /usr/local/bin/docker-php-ext-configure /usr/local/bin/
# COPY --from=php:alpine /usr/local/bin/docker-php-ext-enable /usr/local/bin/
# COPY --from=php:alpine /usr/local/bin/docker-php-ext-install /usr/local/bin/

RUN --mount=type=bind,from=upstream,source=/usr/src,target=/usr/local/src,rw \
  --mount=type=bind,from=upstream,source=/usr/local/bin,target=/opt/bin,rw \
  set -xe \
  && mkdir -p /usr/src \
  && cp /usr/local/src/* /usr/src/ \
  && export PATH=$PATH:/opt/bin \
  ; mkdir -p $PHP_INI_DIR/conf.d \
  && cp /opt/bin/docker-php-entrypoint /usr/local/bin \
  && apk add --no-cache --virtual .build-deps \
          $PHPIZE_DEPS \
          coreutils \
          curl-dev \
          libedit-dev \
          openssl-dev \
          libxml2-dev \
          sqlite-dev \
          cyrus-sasl-dev \
          postgresql-dev \
          # libzip-dev \
          libpng-dev \
          freetype-dev \
          libjpeg-turbo-dev \
          libwebp-dev \
          libxpm-dev \
          libexif-dev \
          # libxslt-dev \
          gmp-dev \
          # xmlrpc-c-dev \
          bzip2-dev \
          enchant-dev \
          imap-dev \
          gettext-dev \
          icu-dev \
          krb5-dev \
          aspell-dev \
          openldap-dev \
          pcre-dev \
          # tidyhtml-dev \
          # net-snmp-dev \
          openldap-dev \
          zlib-dev \
          gnu-libiconv-dev \
          ${APK_DEV_EXTRA:-} \
  \
  && export CFLAGS="$PHP_CFLAGS" \
            CPPFLAGS="$PHP_CPPFLAGS" \
            LDFLAGS="$PHP_LDFLAGS" \
  && docker-php-source extract \
  && rm -vf /usr/include/iconv.h \
  && ln -sv /usr/include/gnu-libiconv/*.h /usr/include/ \
  && cd /usr/src/php \
  && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
  && ./configure \
    --build="$gnuArch" \
    --with-config-file-path="$PHP_INI_DIR" \
    --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
    \
# make sure invalid --configure-flags are fatal errors instead of just warnings
    --enable-option-checking=fatal \
    \
# https://github.com/docker-library/php/issues/439
    --with-mhash \
    \
# --enable-ftp is included here because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236)
    --enable-ftp=shared \
# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
    --enable-mbstring=shared \
# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)
    --enable-mysqlnd=shared \
# https://wiki.php.net/rfc/libsodium
    # --with-sodium=shared \
    \
    --with-curl=shared \
    --with-libedit \
    --with-openssl=shared \
      --with-system-ciphers \
    --with-zlib \
# https://wiki.php.net/rfc/argon2_password_hash (7.2+)
    # --with-password-argon2 \
    \
# bundled pcre does not support JIT on s390x
# https://manpages.debian.org/stretch/libpcre3-dev/pcrejit.3.en.html#AVAILABILITY_OF_JIT_SUPPORT
    $(test "$gnuArch" = 's390x-linux-musl' && echo '--without-pcre-jit') \
    \
    $PHP_EXTRA_CONFIGURE_ARGS \
  && make -j "$(nproc)" \
  && find -type f -name '*.a' -delete \
  && make install \
  && { find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \
# && docker-php-source delete \
  \
  && docker-php-ext-enable bcmath \
                           bz2 \
                           calendar \
                           enchant \
                           # exif \
                           fileinfo \
                           # gd \
                           gettext \
                           gmp \
                           imap \
                           intl \
                           ldap \
                           pcntl \
                           openssl \
                           mysqlnd \
                           # mysqli \
                           pdo \
                           pdo_mysql \
                           pdo_pgsql \
                           pgsql \
                           pspell \
                           shmop \
                           # snmp \
                           # soap \
                           sodium \
                           sockets \
                           sysvmsg \
                           sysvsem \
                           sysvshm \
                           # tidy \
                           # xmlrpc \
                           # xsl \
                           # opcache 已默认安装，需要自行载入
                           opcache \
                           ctype \
                           dom \
                           json \
                           pdo_sqlite \
                           phar \
                           posix \
                           session \
                           simplexml \
                           sqlite3 \
                           tokenizer \
                           xml \
                           xmlreader \
                           xmlwriter \
                           ftp \
                           mbstring \
                           curl \
                           zip \
                           iconv \
                           ${PHP_EXTENSION_EXTRA:-} \
# 7.2+ 不再需要 .so 后缀
  && echo "extension=exif" > $PHP_INI_DIR/conf.d/docker-php-ext-z-exif.ini \
  && echo "extension=gd" > $PHP_INI_DIR/conf.d/docker-php-ext-z-gd.ini \
  && echo "extension=mysqli" > $PHP_INI_DIR/conf.d/docker-php-ext-z-mysqli.ini \
  && runDeps="$( \
    scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
      | tr ',' '\n' \
      | sort -u \
      | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
  )" \
  && apk add --no-cache $runDeps \
  \
# https://github.com/docker-library/php/issues/443
  && pecl update-channels \
  && rm -rf /tmp/pear ~/.pearrc \
  \
# sodium was built as a shared module (so that it can be replaced later if so desired), so let's enable it too (https://github.com/docker-library/php/issues/598)
# RUN docker-php-ext-enable sodium
  \
  && { find $(php-config --extension-dir) -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \
  && strip --strip-all /usr/local/lib/libphp7.so \
  && apk del --no-network .build-deps \
  && rm -rf /usr/local/include/php/TSRM/tsrm_win32.h \
            /usr/local/include/php/TSRM/tsrm_config.w32.h \
            /usr/local/include/php/Zend/zend_config.w32.h \
            /usr/local/include/php/ext/mysqlnd/config-win.h \
            /usr/local/include/php/ext/standard/winver.h \
            /usr/local/php/man \
            /usr/include/*.h \
  # 创建日志文件夹
  && ln -sf /dev/stderr /var/log/xdebug-remote.log \
  && rm -rf /usr/src/*

ENV PECL_BUILD_DEPS \
      libmemcached-dev \
      # yaml-dev \
      zlib-dev \
      zstd-dev \
      pcre-dev

ENV PECL_RUN_DEPS \
      libmemcached-libs \
      # yaml \
      zlib \
      zstd-libs

ENV PECL_EXTENSION \
      mongodb \
      igbinary \
      redis \
      memcached \
      # xdebug \
      # yaml \
      https://github.com/kjdev/php-ext-zstd/archive/0.9.0.tar.gz \
      ${PECL_EXTENSION_EXTRA:-}

RUN --mount=type=bind,from=upstream,source=/usr/local/bin,target=/opt/bin,rw \
      set -x \
      && export PATH=$PATH:/opt/bin \
      && apk add --no-cache --virtual .pecl_build_deps $PECL_BUILD_DEPS $PHPIZE_DEPS \
      && apk add --no-cache --virtual .pecl_run_deps $PECL_RUN_DEPS \
      && curl -fsSL -o /usr/local/bin/pickle \
           https://github.com/khs1994-php/pickle/releases/download/nightly/pickle-debug.phar \
      && chmod +x /usr/local/bin/pickle \
      && echo "--enable-redis-igbinary --enable-redis-zstd" > /tmp/redis.configure.options \
      && echo "--enable-memcached-igbinary" > /tmp/memcached.configure.options \
      && echo "--with-libzstd" > /tmp/zstd.configure.options \
      && pickle install $PECL_EXTENSION -n --defaults --strip --cleanup \
      && pickle install \
         xdebug@3.1.5 \
# https://github.com/tideways/php-xhprof-extension.git
         https://github.com/tideways/php-xhprof-extension/archive/master.tar.gz \
         -n --defaults --no-write --strip --cleanup \
      && apk del --no-network .pecl_build_deps \
      && rm -rf /tmp/* \
      && rm -rf /usr/local/lib/php/.registry/.channel.pecl.php.net/* \
      && php -m \
      && ls -la $(php-config --extension-dir) \
      && php -d error_reporting=22527 -d display_errors=1 -r 'var_dump(iconv("UTF-8", "UTF-8//IGNORE", "This is the Euro symbol '\''€'\''."));'

WORKDIR /app

ENTRYPOINT ["docker-php-entrypoint"]

CMD ["php", "-a"]

ARG VCS_REF="unknow"

LABEL org.opencontainers.image.revision=$VCS_REF \
      org.opencontainers.image.source="https://github.com/khs1994-docker/php"
